package mood.su.data;

import android.content.Context;

import java.sql.SQLException;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mood.su.db.DaoHelper;
import mood.su.utils.DateUtil;

public class MoodDataManager {
    private static MoodDataManager sInstance = new MoodDataManager();
    DaoHelper<DayMood> dmDaoHelper = null;
    DaoHelper<Mood> mDaoHelper = null;
    Context context = null;
    private Map<String, DayMood> mMap;

    public static MoodDataManager getManager(){
        return sInstance;
    }

    public DayMood get(Date date, boolean isNullCreate){
        DayMood mood = null;

        mood = mMap.get(genKeyFromDate(date));

        if((mood == null) && isNullCreate){
            mood = new DayMood(date);
            register(mood);
        }

        return mood;
    }

    public boolean flush(){
        for(Map.Entry<String, DayMood> entry : mMap.entrySet()){
            DayMood dm = entry.getValue();

            if(dm.isDurty()){
                try {
                    List<DayMood> qdm = dmDaoHelper.getDao().queryForEq("date", dm.getDate());
                    if(qdm != null && qdm.size() > 0){

                    }else{
                        dmDaoHelper.create(dm);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            List<Mood> moods = dm.getMoods();
            if(moods != null) {
                for (Mood m : dm.getMoods()) {
                    if (m.isDurty()) {
                        m.setDmId(dm.getId());
                        mDaoHelper.create(m);
                    }
                }
            }
        }

        return true;
    }

    public boolean register(DayMood dayMood){
        boolean success = false;

        if(dayMood != null) {

            DayMood existOne = mMap.get(genKeyFromDate(dayMood.getDate()));

            if(existOne == null) {
                mMap.put(genKeyFromDate(dayMood.getDate()), dayMood);
                success = true;
            }
        }

        return success;
    }

    public void clean(){
        mMap.clear();
    }

    private String genKeyFromDate(Date date){
        DateFormat df = DateFormat.getDateInstance();
        return df.format(DateUtil.getDayWithoutHMS(date));
    }

    private MoodDataManager(){
        mMap = new HashMap<String, DayMood>();
    }

    public boolean load(Context ctx, Date date){
        context = ctx;

        dmDaoHelper = new DaoHelper<DayMood>(context, DayMood.class);
        mDaoHelper = new DaoHelper<Mood>(context, Mood.class);

        return loadDataFromDB(date);
    }

    public boolean reload(Date date){
        return loadDataFromDB(date);
    }

    private boolean loadDataFromDB(Date date){
        boolean success = true;

        try {
            List<DayMood> dmList = dmDaoHelper.getDao().queryForAll();

            for(DayMood dm : dmList){
                register(dm);

                List<Mood> mList = mDaoHelper.getDao().queryForEq("dmId", dm.getId());

                for(Mood m : mList){
                    dm.add(m);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            success = false;
        } finally {
            return success;
        }
    }
}
